package logic;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

/** The class that plays mp3 files.
 * @author Team 1
 *
 */
public class Mp3Player extends Thread {
	
	private URL path;			//The path to the resource.
	private Player player;		//The player.
	
	/** Mp3Player's constructor.
	 * @param fileName : the file to be played with extension.
	 */
	public Mp3Player(String fileName) {
		path = Mp3Player.class.getClassLoader().getResource(fileName);
		
	}
	
	/**Stops the player.
	 */
	public void stopPlayer() {
		player.close();
		this.interrupt();
	}
	
	public void run() {
		try {
			//Plays the resource on loop mode.
			while(!Thread.currentThread().isInterrupted()) {
				InputStream in = path.openStream();
				player = new Player(in);
				player.play();
			}
			player.close();
		} catch (JavaLayerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.interrupt();
	}
}
